/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.foxline.service;

import br.com.foxline.enums.TypeDocument;
import br.com.foxline.model.People;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.NoResultException;
import javax.persistence.NonUniqueResultException;
import javax.persistence.Query;

/**
 *
 * @author roniere
 */
@Stateless
@LocalBean
public class PeopleService extends GenericService<People> {

    public PeopleService() {
        super(People.class);
    }

    public People existPeople(TypeDocument td, String doc) {
        People people;
        String sql = "Select p from People p where ";
        if(td == TypeDocument.CPF){
            sql += " lower(p.cpf) ";
        }else{
            sql += " lower(p.cnpj) ";
        }
        sql += " like lower(:doc)";
        Query query = getEntityManager().createQuery(sql);
        query.setParameter("doc", doc);
        try{
            people = (People) query.getResultList().get(0);
                
        }catch(NoResultException nr){
            nr.printStackTrace();
            return null;
        }catch(NonUniqueResultException nre){
            nre.printStackTrace();
            return null;
        }catch(Exception e){
            e.printStackTrace();
             return null;
        }
        
        
        return people;
    }
}
